package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendUserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询今日佳人(和当前用户缘分值最高的一位)
     * @param loginUserId 登录用户id
     * @return 推荐用户对象
     */
    @Override
    public RecommendUser findRecommendUser(Long loginUserId) {
        //构建查询条件
        //查询推荐给当前用户缘分值最高的用户
        Query query = new Query(Criteria.where("toUSerId").is(loginUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(1L);
            recommendUser.setScore(99D);
        }
        return recommendUser;
    }

    /**
     * 查询推荐用户列表
     * @param loginUserId 登录用户id
     * @param page 页码
     * @param pageSize 每页条数
     * @return 推荐用户集合
     */
    @Override
    public PageBeanVo findRecommendUserList(Long loginUserId, Integer page, Integer pageSize) {
        int startIndex = (page - 1) * pageSize;
        //将缘分值最高的用户跳过,因为今日佳人已经显示,不需要重复显示
        if (page == 1) {
            startIndex = 1;
        }
        Query query = new Query(Criteria.where("toUserId").is(loginUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(startIndex).limit(pageSize);
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        //如果没有推荐用户,模拟一些假数据
        if (CollectionUtil.isEmpty(recommendUserList)) {
            recommendUserList = new ArrayList<>();
            for(int i = 2 ; i < 10 ; i++){
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId( Long.valueOf( i )   ); //设置id
                recommendUser.setScore(RandomUtil.randomDouble(66,99));//随机分数
                recommendUserList.add(recommendUser);
            }
        }
        long count = mongoTemplate.count(query, RecommendUser.class);
        return new PageBeanVo(page,pageSize,count,recommendUserList);
    }

    /**
     * 查询登录用户和推荐用户的缘分值
     * @param loginUserId 登录用户id
     * @param recommendId 推荐用户id
     * @return 推荐用户
     */
    @Override
    public RecommendUser findFateValue(Long loginUserId, Long recommendId) {

        Query query = new Query(Criteria.where("userId").is(recommendId)
                                .and("toUserId").is(loginUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setScore(RandomUtil.randomDouble(66,99));// 跟之前缘分值不一定一样
        }
        return recommendUser;
    }


}
